#!/bin/bash
Deploy_Path=`cd $(dirname $0); pwd -P`
source $Deploy_Path/ceph_cluster.env


# for centos
yum_install_ceph_repo() {
    dnf install -y epel-release
    cp -f $Deploy_Path/repos.d/ceph.repo $Deploy_Path/repos.d/ceph.repo.tmp
    sed -i "s/quincy/$ceph_version/g"  $Deploy_Path/repos.d/ceph.repo.tmp
    cp -f $Deploy_Path/repos.d/ceph.repo.tmp /etc/yum.repos.d/
    dnf clean all && dnf makecache
}

yum_install_ceph_packages() {
     dnf install -y lsscsi parted sed grep  chrony ceph ceph-mgr-dashboard rbd-mirror 

}

apt_install_ceph_repo() {
    sed -i "/.*ceph.*/d" /etc/apt/sources.list
    wget -q -O- 'https://mirrors.aliyun.com/ceph/keys/release.asc' | apt-key add -
    apt-add-repository -y "deb https://mirrors.aliyun.com/ceph/debian-$ceph_version/ $OS_Version main" && apt-get update -yq
}

apt_install_ceph_packages() {
    apt-get install -yq apt-file dpkg pciutils lsscsi parted sed grep chrony ceph ceph-mgr-dashboard rbd-mirror linux-tools-common linux-tools-generic linux-cloud-tools-generic
}

centos_enabled_bcache_module(){

    echo "#！/bin/sh" > /etc/sysconfig/modules/bcache.modules
    echo "/sbin/depmod -A" >> /etc/sysconfig/modules/bcache.modules
    echo "/sbin/modprobe -f bcache" >> /etc/sysconfig/modules/bcache.modules
    chmod 755 /etc/sysconfig/modules/bcache.modules
}

centos_install_bcache_tools(){
    #bash $Deploy_Path/bcache/install_bcache-tools.sh & wait
    #modprobe -f bcache
    mkdir -p /usr/share/initramfs-tools/hooks/
    mkdir -p /usr/lib/initcpio/install/
    mkdir -p /lib/dracut/modules.d/90bcache/

    cp -f $Deploy_Path/bcache/make-bcache /usr/sbin/make-bcache && chmod 755 /usr/sbin/make-bcache
    cp -f $Deploy_Path/bcache/bcache-super-show /usr/sbin/bcache-super-show && chmod 755 /usr/sbin/bcache-super-show
    cp -f $Deploy_Path/bcache/probe-bcache /lib/udev/probe-bcache && chmod 755 /lib/udev/probe-bcache
    cp -f $Deploy_Path/bcache/bcache-register /lib/udev/bcache-register && chmod 755 /lib/udev/bcache-register
    cp -f $Deploy_Path/bcache/69-bcache.rules /lib/udev/rules.d/69-bcache.rules  && chmod 644 /lib/udev/rules.d/69-bcache.rules
    cp -f $Deploy_Path/bcache/hook /usr/share/initramfs-tools/hooks/bcache && chmod 755 /usr/share/initramfs-tools/hooks/bcache
    cp -f $Deploy_Path/bcache/install /usr/lib/initcpio/install/bcache && chmod 755 /usr/lib/initcpio/install/bcache
    cp -f $Deploy_Path/bcache/module-setup.sh /lib/dracut/modules.d/90bcache/module-setup.sh && chmod 755 /lib/dracut/modules.d/90bcache/module-setup.sh
}

ubuntu_install_bcache_tools(){
    apt-get install -yq bcache-tools
}

centos_install_bcache(){
    load_bcache_module
    #centos_enabled_bcache_module
    centos_install_bcache_tools
}

ubuntu_install_bcache(){
    load_bcache_module
    ubuntu_install_bcache_tools
}


install_packages(){
    if [ "$OS_Type"x == ubuntux ];then 
        apt_install_ceph_repo
        apt_install_ceph_packages
    elif [ "$OS_Type"x == centosx ];then
        yum_install_ceph_repo
        yum_install_ceph_packages
    elif [ "$OS_Type"x == opencloudosx ];then
        yum_install_ceph_repo
        yum_install_ceph_packages
    else
        echo "The OS_Type $OS_Type is not supported!"
        exit 1
    fi
}
install_packages
